/**
 * Created by sxwu on 14-2-15.
 */


var fields_all = "adid,userid,adname,adtype,adpicurl,adlinkurl,adpicurl1,adlinkurl1,adpicurl2,adlinkurl2,adhtml,adactivedate,adexpiredate,adflag";

/**
 * 模块依赖
 */
var db = require("../lib/db");

////一个虚拟运营商可能有多条广告，根据广告生效、失效时间判断推送哪一条广告
exports.selectValidAD = function(userid, handler) {
    console.log("selectValidAD");
    db.execQuery({
        "sql": "SELECT * FROM adinfo a,virwifiinfo b WHERE a.userid=? and a.userid=b.userid and a.adactivedate <=now() and a.adexpiredate >now()",
        "args": [userid],
        "handler": handler
    });

};

//查找记录
exports.selectByAdid = function(adid, handler) {
    db.execQuery({
        "sql": "SELECT * " +
            " FROM adinfo a, virwifiinfo b WHERE a.adid=? and a.userid=b.userid",
        "args": [adid],
        "handler": handler
    });

};

//查找记录
exports.selectMPByUserid = function(userid, handler) {
    db.execQuery({
        "sql": "SELECT adid,userid,adname,adtype,adpicurl,adlinkurl,adpicurl1,adlinkurl1,adpicurl2,adlinkurl2,adhtml,adactivedate,adexpiredate,adflag" +
            " FROM adinfo WHERE userid=? and adtype=0",
        "args": [userid],
        "handler": handler
    });

};

//查找记录
exports.selectHTMLByUserid = function(userid, handler) {
    db.execQuery({
        "sql": "SELECT adid,userid,adname,adtype,adpicurl,adlinkurl,adpicurl1,adlinkurl1,adpicurl2,adlinkurl2,adhtml,adactivedate,adexpiredate,adflag" +
            " FROM adinfo WHERE userid=? and adtype=1",
        "args": [userid],
        "handler": handler
    });

};

//查找记录
exports.selectByUserid = function(userid, handler) {
    db.execQuery({
        "sql": "SELECT adid,userid,adname,adtype,adpicurl,adlinkurl,adpicurl1,adlinkurl1,adpicurl2,adlinkurl2,adhtml,adactivedate,adexpiredate,adflag" +
            " FROM adinfo WHERE userid=?",
        "args": [userid],
        "handler": handler
    });

};
//插入记录
exports.insert = function(adinfo, handler) {
    db.execQuery({
        "sql": "insert into adinfo(userid,adname,adtype,adpicurl,adlinkurl,adpicurl1,adlinkurl1,adpicurl2,adlinkurl2,adhtml,adhtmlsrc,adactivedate,adexpiredate,addevtype,adflag) " +
            "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
        "args":[
            adinfo.userid,
            adinfo.adname,
            adinfo.adtype,
            adinfo.adpicurl,
            adinfo.adlinkurl,
            adinfo.adpicurl1,
            adinfo.adlinkurl1,
            adinfo.adpicurl2,
            adinfo.adlinkurl2,
            adinfo.adhtml,
            adinfo.adhtmlsrc,
            adinfo.adactivedate,
            adinfo.adexpiredate,
            adinfo.addevtype,
            0],
        "handler": handler
    });
};

//删除记录
exports.delete = function(adid, handler) {
    db.execQuery({
        "sql": "delete from adinfo where adid in (?)",
        "args":[
            adid],
        "handler": handler
    });
};
//更新记录
exports.update = function(adinfo, handler) {
    db.execQuery({
        "sql": "update adinfo set adname=?,adtype=?,adpicurl=?,adlinkurl=?,adpicurl1=?,adlinkurl1=?,adpicurl2=?,adlinkurl2=?,adhtml=?,adhtmlsrc=?,adactivedate=?,adexpiredate=?,addevtype=? WHERE adid=?",
        "args": [
            //adinfo.userid,
            adinfo.adname,
            adinfo.adtype,
            adinfo.adpicurl,
            adinfo.adlinkurl,
            adinfo.adpicurl1,
            adinfo.adlinkurl1,
            adinfo.adpicurl2,
            adinfo.adlinkurl2,
            adinfo.adhtml,
            adinfo.adhtmlsrc,
            adinfo.adactivedate,
            adinfo.adexpiredate,
            adinfo.addevtype,
            adinfo.adid
        ],
        "handler": handler
    });

};

//遍历记录
exports.findAll = function(handler) {
    db.execQuery({
        "sql": "SELECT * FROM adinfo ORDER BY adid asc",
        "handler": handler
    });
};
